import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getStoreIdNameMap } from '@/api/baseconfig/store'
import { modPassword } from '@/api/login'
import store from '../index'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    stores: [],
    currentStore: '',
    currentStoreName: '',
    userInfo: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_STORES: (state, stores) => {
      state.stores = stores
    },
    SET_CURRENTSTORE: (state, currentStore) => {
      state.currentStore = currentStore
    },
    SET_CURRENTSTORENAME: (state, currentStoreName) => {
      state.currentStoreName = currentStoreName
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo.username, userInfo.password).then(response => {
          commit('SET_TOKEN', response.data.tokenValue)
          setToken(response.data.tokenValue)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      //获取门店信息
      return new Promise((resolve, reject) => {
        getInfo().then(resp => {
          const data = {
            roles: ['Admin'],
            introduction: 'I am a super administrator',
            avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
            name: 'Super Admin'
          }
          if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data.roles)
          } else {
            reject('getInfo: roles must be a non-null array !')
          }
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_USERINFO', resp.data)
          resolve()
        }).catch(e => {
          reject()
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout().then(response => {
          commit('SET_ROLES', [])
          commit('SET_TOKEN', '')
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resolve()
      })
    },
    //修改秘密
    modPassword({ commi }, user) {
      return modPassword(user)
    },
    //门店
    getStores({ commit, state }) {
      getStoreIdNameMap().then(resp => {
        commit('SET_STORES', resp.data)
        if (sessionStorage.storeName === '全部') {
          commit('SET_CURRENTSTORE', '')
          commit('SET_CURRENTSTORENAME', '全部')
        } else {
          if (sessionStorage.storeName) {
            commit('SET_CURRENTSTORE', parseInt(sessionStorage.storeId))
            commit('SET_CURRENTSTORE', sessionStorage.storeName)
          } else {
            // commit('SET_CURRENTSTORE', resp.data[0].id)
            // commit('SET_CURRENTSTORE', resp.data[0].name)
            // sessionStorage.storeId = resp.data[0].id
            // sessionStorage.storeName = resp.data[0].name
          }
        }

      })
    }
  }
}

export default user
